package org.br.erp.base.service.impl;

import org.br.erp.base.entity.Scmfilter;
import org.br.erp.base.entity.Scmfilteritem;
import org.br.erp.base.mapper.ScmfilteritemMapper;
import org.br.erp.base.mapper.ScmfilterMapper;
import org.br.erp.base.service.IScmfilterService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 表单过滤条件
 * @Author: jeecg-boot
 * @Date:   2023-08-12
 * @Version: V1.0
 */
@Service
public class ScmfilterServiceImpl extends ServiceImpl<ScmfilterMapper, Scmfilter> implements IScmfilterService {

	@Autowired
	private ScmfilterMapper scmfilterMapper;
	@Autowired
	private ScmfilteritemMapper scmfilteritemMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(Scmfilter scmfilter, List<Scmfilteritem> scmfilteritemList) {
		scmfilterMapper.insert(scmfilter);
		if(scmfilteritemList!=null && scmfilteritemList.size()>0) {
			for(Scmfilteritem entity:scmfilteritemList) {
				//外键设置
				entity.setParentid(scmfilter.getId());
				scmfilteritemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(Scmfilter scmfilter,List<Scmfilteritem> scmfilteritemList) {
		scmfilterMapper.updateById(scmfilter);
		
		//1.先删除子表数据
		scmfilteritemMapper.deleteByMainId(scmfilter.getId());
		
		//2.子表数据重新插入
		if(scmfilteritemList!=null && scmfilteritemList.size()>0) {
			for(Scmfilteritem entity:scmfilteritemList) {
				//外键设置
				entity.setParentid(scmfilter.getId());
				scmfilteritemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		scmfilteritemMapper.deleteByMainId(id);
		scmfilterMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			scmfilteritemMapper.deleteByMainId(id.toString());
			scmfilterMapper.deleteById(id);
		}
	}
	
}
